import upperFirst from 'lodash/upperFirst';
import camelCase from 'lodash/camelCase';
// https://webpack.js.org/guides/dependency-management/#require-context

export default function autoReisterGlobal (app) {
    const requireComponent = require.context(
        // Look for files in the current directory
        './components/_wp_components',
        // Do not look in subdirectories
        false,
        // Only include "_base-" prefixed .vue files
        /[\w-]+\.vue$/
    );

    // For each matching file name...
    requireComponent.keys().forEach((fileName) => {
        // Get the component config
        const componentConfig = requireComponent(fileName);
        // Get the PascalCase version of the component name
        const componentName = upperFirst(
            camelCase(
                // 获取和目录深度无关的文件名
                fileName
                    .split('/')
                    .pop()
                    .replace(/\.\w+$/, '')
            )
        );

        // Globally register the component
        app.component(componentName, componentConfig.default || componentConfig);
    });
}
